package com.sxx.beautifulcity.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.sxx.beautifulcity.application.ProjectApplication;
import com.sxx.beautifulcity.entity.greendao.AgritourismDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.AroundDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.BarbecuteDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.DaoMaster;
import com.sxx.beautifulcity.entity.greendao.DaoSession;
import com.sxx.beautifulcity.entity.greendao.DetailDao;
import com.sxx.beautifulcity.entity.greendao.HolidayDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.KeepHealthDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.LineDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.LoginUserDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.PluckDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.UserDaoEntityDao;
import com.sxx.beautifulcity.entity.greendao.VillageDaoEntityDao;

/**
 * Created by dllo on 15/11/2.
 */
public class DaoSingleton {

    private static final String DATABASE_NAME = "beautifulcity.db";

    private volatile static DaoSingleton instance;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private AgritourismDaoEntityDao agritourismDao;//农家院
    private HolidayDaoEntityDao holidayDao;//度假村
    private PluckDaoEntityDao pluckDao;//采摘
    private VillageDaoEntityDao villageDao;//乡村
    private LineDaoEntityDao lineDao;//精品线路
    private AroundDaoEntityDao aroundDao;//
    private BarbecuteDaoEntityDao barbecuteDao;//烧烤
    private KeepHealthDaoEntityDao keepHealthDao;//养生
    private UserDaoEntityDao userDao;//用户
    private LoginUserDaoEntityDao loginUserDao;//登录表
    private DetailDao detailDao;//详情表


    private DaoSingleton() {
        context = ProjectApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        return SingletonHolder.instance;
    }
    private static final class SingletonHolder{
        private static final DaoSingleton instance = new DaoSingleton();
    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    public SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    /**
     * 农家院
     * @return
     */
    public AgritourismDaoEntityDao getAgritourismDao(){
        if (agritourismDao == null) {
            agritourismDao = getDaoSession().getAgritourismDaoEntityDao();
        }
        return agritourismDao;
    }

    /**
     * 度假村
     * @return
     */
    public HolidayDaoEntityDao getHolidayDao(){
        if (holidayDao == null) {
            holidayDao = getDaoSession().getHolidayDaoEntityDao();
        }
        return holidayDao;
    }

    /**
     * 乡村
     * @return
     */
    public VillageDaoEntityDao getVillageDao(){
        if (villageDao == null) {
            villageDao = getDaoSession().getVillageDaoEntityDao();
        }
        return villageDao;
    }

    /**
     * 精品线路
     * @return
     */
    public LineDaoEntityDao getLineDao(){
        if (lineDao == null) {
            lineDao = getDaoSession().getLineDaoEntityDao();
        }
        return lineDao;
    }

    /**
     * 景区附近
     * @return
     */
    public AroundDaoEntityDao getAroundDao(){
        if (aroundDao == null) {
            aroundDao = getDaoSession().getAroundDaoEntityDao();
        }
        return aroundDao;
    }

    /**
     * 采摘
     * @return
     */
    public PluckDaoEntityDao getPluckDao(){
        if (pluckDao == null) {
            pluckDao = getDaoSession().getPluckDaoEntityDao();
        }
        return pluckDao;
    }

    /**
     *烧烤
     * @return
     */
   public BarbecuteDaoEntityDao getBarbecuteDao(){
       if (barbecuteDao == null) {
           barbecuteDao = getDaoSession().getBarbecuteDaoEntityDao();
       }

       return  barbecuteDao;
   }

    /**
     * 养生
     * @return
     */
    public KeepHealthDaoEntityDao getKeepHealthDao(){
        if (keepHealthDao == null) {
            keepHealthDao = getDaoSession().getKeepHealthDaoEntityDao();
        }

        return  keepHealthDao;
    }

    /**
     * 用户
     * @return
     */
    public UserDaoEntityDao getUserDao() {
        if (userDao == null) {
            userDao = getDaoSession().getUserDaoEntityDao();
        }
        return userDao;
    }

    /***
     * 用户登录表
     * @return
     */

    public LoginUserDaoEntityDao getLoginUserDao(){
        if (loginUserDao == null) {
            loginUserDao = getDaoSession().getLoginUserDaoEntityDao();
        }
        return loginUserDao;
    }
    /***
     * 详情表
     */
    public DetailDao getDetailDao(){
        if (detailDao == null) {
            detailDao =  getDaoSession().getDetailDao();
        }
        return detailDao;
    }
}
